package bs.app.android;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Image {

	public Bitmap bmp = null;
	private Graphics g = null;
	
	public Image(Bitmap bm){
		bmp = bm;
	}
	
	public static Image createImage(int w, int h){
		Bitmap tmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		if(tmp != null)
			return new Image(tmp);
		return null;
	}
	public static Image createImageFromFile(String filePath){
		Bitmap tmp = BitmapFactory.decodeFile(filePath);
		if(tmp != null)
			return new Image(tmp);
		return null;
	}
	public static Image createRGBImage(int[] rgb, int width, int height, boolean processAlpha){
		Bitmap tmp = Bitmap.createBitmap(rgb,width, height, processAlpha ? Bitmap.Config.ALPHA_8 : Bitmap.Config.RGB_565);
		if(tmp != null)
			return new Image(tmp);
		return null;
	}
	public static Image createImage(byte[] data, int offset, int length) throws IOException {
		Bitmap tmp = BitmapFactory.decodeByteArray(data, offset, length);
		if(tmp != null)
			return new Image(tmp);
		return null;
	}
	public static Image createImage(String sRes) throws IOException {
		int b = sRes.lastIndexOf('/');
		int e = sRes.lastIndexOf('.');
		if(e==0) e = sRes.length();
		if(e-b>1)
			sRes = sRes.substring(b+1, e);
		else
			return null;

		int id = Static.App.getResources().getIdentifier(sRes, "drawable", Static.App.getPackageName());
		if(id==0) return null;
		Bitmap tmp = BitmapFactory.decodeResource(Static.App.getResources(), id);
		if(tmp != null)
			return new Image(tmp);
		return null;
	}	
	public int getWidth(){
		return bmp.getWidth();
	}
	public int getHeight(){
		return bmp.getHeight();
	}
	
	public Graphics getGraphics(){
		if(g == null && bmp.isMutable()){ // bmp must is mutable with: static Bitmap createBitmap(int width, int height, Bitmap.Config config)
			g = new Graphics(new Canvas(bmp));
		}
		return g;
	}

	public void getRGB(int[] data, int i, int s_iWcanvas, int j, int k,
			int s_iWcanvas2, int s_iHcanvas) {
		// TODO Auto-generated method stub
		//System.out.println("Chua viet Image >> getRGB");
	}
}
